#include "PreCompile.h"
#include "ContentException.h"
#include "../nodes/node.h"
#include "../document.h"

namespace Xml
{
  //////////
  // ContentException
  ContentException::ContentException(const Node &node,const Glib::ustring &msg)
  {
    line = node.get_line();
    message = msg;
    file_name = node.get_document()->get_file_name();

    full_message_dirty = true;
  }

  ContentException::~ContentException() throw()
  {
  }

  void ContentException::format_message() const
  {
    std::stringstream msg;

    bool need_space = false;
    if(file_name != "")
    {
      msg << file_name << ":";
      need_space = true;
    }

    if(line != -1)
    {
      msg << line << ":";
      need_space = true;
    }

    if(need_space)
      msg << " ";
    msg << message;

    full_message_dirty = false;
    full_message = msg.str();
  }

  const char* ContentException::what() const throw()
  {
    if(full_message_dirty)
      format_message();

    return full_message.c_str();
  }

  Exception* ContentException::Clone() const
  {
    return new ContentException(*this);
  }

  //////////////
  // AttributeException
  AttributeException::AttributeException(const Element &element,
    const Glib::ustring &attrib_name,const Glib::ustring &msg)
    : ContentException(get_attribute(element,attrib_name),msg)
  {
  }

  const Attribute& AttributeException::get_attribute(const Element &element,
    const Glib::ustring &attrib_name)
  {
    const Attribute *ret = element.try_get_attribute(attrib_name);
    assert(ret);
    return *ret;
  }
}
